package com.asset.servlet;
/**
 *@ClassName LoginServlet
 *@Author B21015118吴逸鑫
 *@Date 2022/6/9 08:10
 **/
/**
 * 登录
 * **/

import com.asset.bean.User;
import com.asset.dao.UserDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserDAO userDAO = new UserDAO();
        User user = userDAO.login(username, password);
        HttpSession session = request.getSession();
        session.setAttribute("user", user);
        /**
         * 注册成功进入欢迎页面
         * 注册失败进入登陆失败页面
         * **/
        if(user != null){
            request.getRequestDispatcher("index.jsp").forward(request,response);
        } else{
            request.getRequestDispatcher("login_error.jsp").forward(request, response);

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
